<template>
  <div class="page">
    <!-- <el-form :model="searchForm" label-position="left" inline label-suffix="：" size="small">
      <div class="flex wap flex-start">
        <el-form-item label="名称">
          <el-input v-model="searchForm.name"></el-input>
        </el-form-item>
        <el-form-item label="手机号码">
          <el-input v-model="searchForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="">
          <el-button type="primary" @click="getData">查询</el-button>
        </el-form-item>
      </div>
    </el-form> -->
    <div class="flex wap flex-start" style="padding: 0px 0 10px 0">
      <el-button type="success" @click="add">新增</el-button>
      <div style="width: 20px"></div>
      <!-- <el-button type="danger" @click="del">删除</el-button> -->
    </div>
    <el-table
      :data="tableData"
      border
      style="width: 100%"
      height="600"
      :header-cell-style="{
        background: '#eef1f6',
        color: '#606266',
        'text-align': 'center',
      }"
      :cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column prop="name" label="考试名称"></el-table-column>kais
      <el-table-column prop="lessionname" label="课程名称"></el-table-column>
      <el-table-column prop="classname" label="班级名称"></el-table-column>
      <el-table-column prop="starttime" label="开始时间"></el-table-column>
      <el-table-column prop="endtime" label="结束时间"></el-table-column>
      <el-table-column prop="status" label="状态" width="100">
        <template slot-scope="scop">
          <el-tag type="warning" v-if="scop.row.status == 0">未完成</el-tag>
          <el-tag type="success" v-if="scop.row.status == 1">已完成</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="" label="操作" width="180">
        <template slot-scope="scop">
          <el-button
            type="primary"
            icon="el-icon-edit"
            circle
            @click="edit(scop.row)"
          ></el-button>
          <el-button
            type="success"
            icon="el-icon-document"
            circle
            @click="testScore(scop.row)"
          ></el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            circle
            @click="del(scop.row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="float: right; padding: 10px">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[10, 20, 50, 100, 200]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog
      title="新增/修改考试"
      center
      :visible.sync="dialogFormVisible"
      width="40%"
    >
      <el-form
        :model="addOrEditForm"
        ref="RefAddOrEditForm"
        :rules="rule"
        label-width="100px"
        class="edit-form"
        label-suffix="："
      >
        <el-form-item label="考试名称" prop="name">
          <el-input
            v-model="addOrEditForm.name"
            placeholder="请输入考试名称"
            maxlength="20"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="选择班级" prop="classid">
          <check-class
            :itemId="addOrEditForm.classid"
            @checkedObj="checkedclass"
          />
        </el-form-item>
        <el-form-item label="选择课程" prop="lessionid">
          <check-lession
            :itemId="addOrEditForm.lessionid"
            @checkedObj="checkedLession"
          />
        </el-form-item>
        <el-form-item label="考试时间" prop="classTime">
          <el-date-picker
            v-model="addOrEditForm.classTime"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="~"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="addOrEditForm.status">
            <el-radio :label="0">未完成</el-radio>
            <el-radio :label="1">已完成</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="考试成绩" center :visible.sync="scoreVisible" width="50%">
      <div class="flex flex-between">
        <h3>
          {{
            "【" +
            testOBJ.classname +
            "】-" +
            "【" +
            testOBJ.name +
            "】-" +
            "【" +
            testOBJ.lessionname +
            "】"
          }}
        </h3>
        <div>
          <span style="margin-right:30px">考试时间：{{ testOBJ.starttime }}</span>
          <el-button type="danger" size="mini" @click="delTScore"
            >清空成绩</el-button
          >
          <el-button type="success" size="mini"
            >导出</el-button
          >
        </div>
      </div>
      <el-table
        :data="testScoreTable"
        border
        style="width: 100%"
        max-height="600"
        :header-cell-style="{
          background: '#eef1f6',
          color: '#606266',
          'text-align': 'center',
        }"
        :cell-style="{ 'text-align': 'center' }"
      >
        <el-table-column
          prop="stuname"
          label="学生姓名"
          width="100"
        ></el-table-column>
        <el-table-column prop="scorevalue" label="分数" width="100">
          <template slot-scope="scop">
            <el-input
              v-model="scop.row.scorevalue"
              size="mini"
              v-if="scop.row.edit == true"
            ></el-input>
            <span v-else>{{ scop.row.scorevalue }}</span>
          </template>
        </el-table-column>
        
        <el-table-column
          prop="empname"
          label="教师"
          width="100"
        ></el-table-column>
        <el-table-column prop="comment" label="评语" show-overflow-tooltip max-width="200">
          <template slot-scope="scop">
            <el-input
              type="textarea"
              v-model="scop.row.comment"
              size="mini"

              v-if="scop.row.edit == true"
            ></el-input>
            <span v-else>{{ scop.row.comment }}</span>
          </template>
        </el-table-column>

        <!-- 
      <el-table-column prop="status" label="状态" width="100">
        <template slot-scope="scop">
          <el-tag type="warning" v-if="scop.row.status == 0">未完成</el-tag>
          <el-tag type="success" v-if="scop.row.status == 1">已完成</el-tag>
        </template>
      </el-table-column> -->
        <el-table-column prop="" label="操作" width="80">
          <template slot-scope="scop">
            <el-button
              type="warning"
              icon="el-icon-edit"
              circle
              size="mini"
              @click="editScore(scop.row)"
              v-if="scop.row.edit == false"
            ></el-button>
            <el-button
              v-else
              type="success"
              icon="el-icon-finished"
              circle
              size="mini"
              @click="saveStuScore(scop.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="scoreVisible = false">返 回</el-button>
        <!-- <el-button type="primary" @click="saveScore">保 存</el-button> -->
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  findTest,
  addTest,
  updateTest,
  delTest,
  findTestScore,
  updateTestScore,
  delTestScore,
} from "@/api/user";
import upload from "@/components/uploadFile";
import checkLession from "@/views/components/checkLession";
import checkClass from "@/views/components/checkClass";
export default {
  components: {
    upload,
    checkLession,
    checkClass,
  },
  data() {
    return {
      total: 0,
      pageNum: 1,
      pageSize: 10,
      tableData: [],
      testScoreTable: [],
      testOBJ: {},
      searchForm: {
        name: "",
        phone: "",
      },
      dialogFormVisible: false,
      scoreVisible: false,
      addOrEditForm: {
        name: "",
        testid: "",
        lessionid: "",
        classid: "",
        status: 0,
        classTime: [],
      },
      rule: {
        name: [
          { required: true, message: "请输入课程名称", trigger: "change" },
        ],
        lessionid: [
          { required: true, message: "请选择课程", trigger: "change" },
        ],
        classid: [{ required: true, message: "请选择班级", trigger: "change" }],
        classTime: [
          { required: true, message: "请选择时间", trigger: "change" },
        ],
      },
    };
  },
  computed: {
    queryData() {
      return {
        pageNum: this.pageNum,
        pageSize: this.pageSize,
      };
    },
  },
  mounted() {
    this.getData();
  },
  methods: {
    async getData() {
      const res = await findTest.post(this.queryData);
      if (res.resultCode === "000000") {
        this.tableData = res.data.classList;
        this.total = res.data.count;
      } else {
        this.$message.error(res.resultInfo);
      }
    },
    edit(row) {
      this.dialogFormVisible = true;
      const { name, lessionid, testid, status, classid, starttime, endtime } =
        row;
      this.addOrEditForm = {
        name,
        testid,
        status,
        lessionid,
        classid,
        classTime: [starttime, endtime],
      };
    },
    checkedLession(val) {
      this.addOrEditForm.lessionid = val.lessionid;
    },
    checkedclass(val) {
      this.addOrEditForm.classid = val.classid;
    },
    add() {
      this.dialogFormVisible = true;
      this.addOrEditForm = {
        name: "",
        status: 0,
        testid: "",
        lessionid: "",
        classid: "",
        classTime: [],
      };
    },
    save() {
      this.$refs.RefAddOrEditForm.validate(async (valid) => {
        if (valid) {
          const { name, lessionid, status, classid, testid, classTime } =
            this.addOrEditForm;
          const addData = {
            name,
            lessionid,
            classid,
            status,
            testid,
            starttime: classTime[0],
            endtime: classTime[1],
          };
          if (this.addOrEditForm.testid === "") {
            const res = await addTest.post(addData);
            if (res.resultCode === "000000") {
              this.$message.success(res.resultInfo);
              this.dialogFormVisible = false;
              this.getData();
            } else {
              this.$message.error(res.resultInfo);
            }
          } else {
            const res = await updateTest.post(addData);
            if (res.resultCode === "000000") {
              this.$message.success(res.resultInfo);
              this.dialogFormVisible = false;
              this.getData();
            } else {
              this.$message.error(res.resultInfo);
            }
          }
        } else {
          return false;
        }
      });
    },
    del(row) {
      this.$confirm("此操作将删除该项目, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.delImg(row.testid);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    async delImg(lessionid) {
      const res = await delTest.post({ lessionid });
      if (res.resultCode === "000000") {
        this.$message.success("删除成功！");
        this.getData();
      } else {
        this.$message.error(res.resultInfo);
      }
    },
    testScore(val) {
      this.scoreVisible = true;
      this.testOBJ = val;
      console.log(this.testOBJ, 520);
      this.getScore();
    },
    async getScore() {
      const res = await findTestScore.post({testid:this.testOBJ.testid});
      if (res.resultCode === "000000") {
        res.data.forEach((el) => {
          el.edit = false;
        });
        this.testScoreTable = res.data;
      } else {
        this.$message.error(res.resultInfo);
      }
    },
    editScore(row) {
      row.edit = true;
    },
    async saveStuScore(row) {
      const datas = {
        comment: row.comment,
        testid: row.testid,
        stuid: row.stuid,
        scorevalue: row.scorevalue,
        scoreid:row.scoreid
      };
      const res = await updateTestScore.post(datas);
      if (res.resultCode === "000000") {
        row.edit = false;
      } else {
        this.$message.error(res.resultInfo);
      }
    },
    saveScore() {},
    async delTScore() {
      const res = await delTestScore.post({ testid: this.testOBJ.testid });
      if (res.resultCode === "000000") {
        this.getScore();
      } else {
        this.$message.error(res.resultInfo);
      }
    },
    handleSizeChange(val) {
      this.pageNum = 1;
      this.pageSize = val;
      this.getData();
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.getData();
    },
  },
};
</script>
<style lang="less" scoped></style>
